/*
 * Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2023-2023. All rights reserved.
 */

#include "ComboBoxCom.h"
#include <QListView>

namespace BurnDlg {
ComboBoxCom::ComboBoxCom(QWidget *parent)
    : QComboBox(parent)
{
    QListView* view = new QListView();
    setView(view);
    this->view()->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    this->view()->setTextElideMode(Qt::ElideNone);
}

ComboBoxCom::~ComboBoxCom()
{
}

void ComboBoxCom::mousePressEvent(QMouseEvent *e)
{
    if (e == nullptr) {
        return;
    }
    if (e->button() == Qt::LeftButton) {
        emit Clicked();
        this->SetViewWidth();
    }

    QComboBox::mousePressEvent(e);
}

void ComboBoxCom::SetViewWidth() const
{
    int maxLen = 0;
    for (int i = 0; i < count(); i++) {
        if (maxLen < itemText(i).length()) {
            maxLen = itemText(i).length();
        }
    }
    int ptVal = this->font().pointSize();
    int width = static_cast<int>(maxLen * ptVal * 0.75);
    if (width < this->width()) {
        width = this->width();
    }
    view()->setFixedWidth(width);
}

}
